/**
 * Filename: DraweePagerAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/13 16:41
 */
package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.imagepipeline.common.ResizeOptions;
import com.github.postsanf.yinian.bean.YNNativeImage;
import com.github.postsanf.yinian.bean.YNPhoto;
import com.github.postsanf.yinian.bean.YNPicList;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.view.FrePhotoView;
import com.liulishuo.qiniuimageloader.QiniuImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * 大图浏览适配器
 */
public class DraweePagerAdapter extends PagerAdapter {

    private Activity mContext;
    private List<String> mUrls;
    private Boolean isLocal = false;

    public DraweePagerAdapter(Activity mContext, List<String> mUrls, Boolean isLocal) {
        this.mContext = mContext;
        this.mUrls = mUrls;
        this.isLocal = isLocal;
    }

    public DraweePagerAdapter(Activity mContext, List<YNNativeImage> photos) {
        this.mContext = mContext;
        List<String> urls = new ArrayList<>();
        for (int i = 0; i < photos.size(); i++) {
            urls.add(photos.get(i).getPath());
        }
        this.mUrls = urls;
    }

    public DraweePagerAdapter(Activity mContext, List<YNPicList> ynPicListList, String a) {
        this.mContext = mContext;
        List<String> urls = new ArrayList<>();
        if (ynPicListList == null || ynPicListList.size() == 0)
            return;
        for (int i = 0; i < ynPicListList.size(); i++) {
            urls.add(ynPicListList.get(i).getPoriginal());
        }
        this.mUrls = urls;
    }

    public void removeItem(int position) {
        mUrls.remove(position);
        notifyDataSetChanged();
    }

    public DraweePagerAdapter(Activity mContext, List<YNPhoto> photos, String a, String b) {
        this.mContext = mContext;
        List<String> urls = new ArrayList<>();
        for (int i = 0; i < photos.size(); i++) {
            urls.add(photos.get(i).getUrl());
        }
        this.mUrls = urls;
    }

    @Override
    public int getCount() {
        return mUrls.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        final String url = isLocal ? "file://" + Uri.parse(mUrls.get(position)) : StringUtils.repaceImgUrl(new QiniuImageLoader(mContext, mUrls.get(position)).halfScreenW().formatWebp().createQiniuUrl());
//        final String url = isLocal ? "file://" + Uri.parse(mUrls.get(position)) : mUrls.get(position);
        Log.i("xjs", "urllll===>" + url);
        final FrePhotoView photoDraweeView = new FrePhotoView(mContext);
        photoDraweeView.setImageUri(url, new ResizeOptions(DisplayUtils.dp2px(mContext, 400), DisplayUtils.dp2px(mContext, 400)), mContext);
//        photoDraweeView.setImageUri2(mContext,url, null);
//        photoDraweeView.setImageUri3(url,mContext);
        container.addView(photoDraweeView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        return photoDraweeView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    public String getPic(int position) {
        return mUrls.size() > 0 ? mUrls.get(position % mUrls.size()) : "";
    }

//    public Bitmap getBitmap(int position) {
//        Bitmap bitmap = null;
//        FrePhotoView view = (FrePhotoView) mViews.get(position % mViews.size());
//        Drawable drawable = view.getDrawable();
//        if (drawable != null && drawable instanceof BitmapDrawable) {
//            BitmapDrawable bd = (BitmapDrawable) drawable;
//            bitmap = bd.getBitmap();
//        }
//        return bitmap;
//    }
}
